package com.gy.wx_demo_mg.utils;

import java.util.HashMap;

/**
 * @author GY
 * 2017年12月27日
 * 响应前端json对象
 */
public class RSon {
    
    /**
     * 响应码
     */
    private String code;
    /**
     * 响应信息
     */
    private String msg;
    /**
     * 响应数据
     */
    private Object data;
    
    /**
     * 返回码--返回信息映射map
     */
    private static final HashMap<String, String> RMAP = new HashMap<String, String>();
    /**
     * 操作成功
     */
    public static final String SUCCESS_CODE = "1111";
    /**
     * 操作失败
     */
    public static final String FAILD_CODE = "0000";
    /**
     * 操作异常
     */
    public static final String ERROR_CODE = "9999";
    
    /**
     * 参数不能为空
     */
    public static final String EMPTY_ERROR_CODE = "0001";
    
    /**
     * 数据库操作异常
     */
    public static final String DAO_ERROR_CODE = "0002";
    
    /**
     * 用户不存在
     */
    public static final String USER_ERROR_CODE = "0003";
    
    /**
     * 验证码错误
     */
    public static final String CODE_ERROR = "0004";
    
    /**
     * 用户名已经存在
     */
    public static final String USER_NAME_EXIST_ERROR = "0005";
    
    /**
     * 密码错误
     */
    public static final String USER_PASSWORD_ERROR = "0006";
    
    private static final RSon SUCCESS_RSON = new RSon(SUCCESS_CODE);
    private static final RSon ERROR_RSON = new RSon(ERROR_CODE);
    
    static{
        RMAP.put(SUCCESS_CODE, "操作成功");
        RMAP.put(FAILD_CODE, "操作失败");
        RMAP.put(ERROR_CODE, "操作异常");
        RMAP.put(EMPTY_ERROR_CODE, "参数不能为空");
        RMAP.put(DAO_ERROR_CODE, "操作失败");
        RMAP.put(USER_ERROR_CODE, "用户不存在");
        RMAP.put(CODE_ERROR, "验证码错误");
        RMAP.put(USER_NAME_EXIST_ERROR, "用户名已经存在");
        RMAP.put(USER_PASSWORD_ERROR, "密码错误");
    }
    
    public RSon() {
        
    }
    
    public RSon(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    
    public RSon(String code) {
        this.code = code;
        this.msg = RMAP.get(code);
    }
    
    public RSon(String code,Object data) {
        this.code = code;
        this.data = data;
        this.msg = RMAP.get(code);
    }
    
    /**
     * @return
     * GY
     * 2017年12月27日
     * 使用静态对象 注意 线程安全
     */
    public static RSon success(){
        return SUCCESS_RSON;
    }
    
    public static RSon success(Object data){
        return new RSon(SUCCESS_CODE,data);
    }
    
    /**
     * @return
     * GY
     * 2017年12月27日
     * 使用静态对象 注意 线程安全
     */
    public static RSon error(){
        return ERROR_RSON;
    }
    
    public static RSon error(String code){
        RSon r = new RSon();
        r.setCode(code);
        r.setMsg(RMAP.get(code));
        return r;
    }
    
    public static void error(RSon r,String code){
        r.setCode(code);
        r.setMsg(RMAP.get(code));
    }
    
    public static RSon error(String code,String msg){
        RSon r = new RSon();
        r.setMsg(msg);
        r.setCode(code);
        return r;
    }
    
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    
}
